home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / clm89nov.arc / STITT.C < prev    next >
Text File  |  1989-12-25  |  7KB  |  200 lines

  1.  
  2. Listing 1 - This sample C program illustrates how an application can
  3. interface with the PC-MOS operating system.
  4.  
  5. /*
  6. tref.c - sample program for PC-MOS Tech Ref
  7. (c) Copyright 1989 The Software Link, Incorporated
  8. */
  9.  
  10. /* === Include files ===================*/
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <bios.h>
  16.  
  17. #define tcbid      0x0010     /*dw task id (task0 = 0, task1 = 1, etc.)  */
  18.  
  19. /*=== Macros ==========================*/
  20.  
  21. #define extcall int86x(extvect,®s,®s,&sregs)
  22. #define i21call int86x(0x21,®s,®s,&sregs)
  23.  
  24. /*=== Typedefs ========================*/
  25.  
  26. typedef unsigned char byte;
  27. typedef int word;
  28.  
  29. #define ofs 0
  30. #define seg 1
  31. typedef union {
  32.   int hlf[2];                   /* access to each half of the pointer */
  33.   unsigned char far *bptr;      /* use the pointer for a byte operation */
  34.   unsigned int far *wptr;       /* use the pointer for a word operation */
  35.   void far *vptr;               /* used for pointer to pointer copies */
  36.   long longint;                 /* used for double word numbers */
  37.   } dword;
  38.  
  39. /*=== Structures ======================*/
  40.  
  41. /*
  42. the following structure outlines the data fields which must
  43. be provided for the addtask API function call
  44. */
  45.  
  46. typedef struct {
  47.   word tsize;           /* task size */
  48.   word tid;             /* task id */
  49.   byte tclass;          /* task class */
  50.   dword tbatch;         /* task startup batchfile */
  51.   dword tdriver;        /* task terminal driver */
  52.   word tport;           /* task port */
  53.   dword tbaud;          /* task baud rate */
  54.   dword tmemtot;        /* total ext mem     (RETURN) */
  55.   dword tmemalc;        /* ext mem allocated (RETURN) */
  56.   word tsmpal;          /* task smp allocate (RETURN) */
  57.   word tsmpsiz;         /* task smp size     (RETURN) */
  58.   word tpercent;        /* task percent heap (RETURN) */
  59.   byte tres[3];         /* reserved */
  60.   } add_data;
  61.  
  62. /*=== Global variables ================*/
  63.  
  64.   union REGS regs; struct SREGS sregs;
  65.   word extvect;         /* vector number for extended services */
  66.   add_data addparm;     /* declare an add_data structured variable */
  67.   char strtbat[9] = "";                 /* pointer to startup batch file */
  68.  
  69. /*=== Subroutines =====================*/
  70.  
  71. /*
  72. ;- WORD_FROM_TCB -------------------------------------------------------;
  73. ; entry parms:    tcb segment and offset                    ;
  74. ; exit parms:    word from tcb                        ;
  75. ;-----------------------------------------------------------------------;
  76. */
  77. word word_from_tcb(word tseg, word tofs) {
  78.  
  79.   dword xptr;
  80.  
  81.   xptr.hlf[seg] = tseg;
  82.   xptr.hlf[ofs] = tofs;
  83.   return(*xptr.wptr);
  84.   }
  85.  
  86. /*
  87. ;- GET_VECTS -----------------------------------------------------------;
  88. ; entry parms:  none                                                    ;
  89. ; exit parms:   the global variable extvect is initialized              ;
  90. ;                                                                       ;
  91. ; NOTE: the extvect pointer should not be considered valid until after  ;
  92. ; the ismos procedure has been used to verify that MOS is the OS.       ;
  93. ;-----------------------------------------------------------------------;
  94. */
  95. void get_vects() {
  96.  
  97.   extvect = 0x38;               /* default choice */
  98.   regs.x.ax = 0x3000;
  99.   regs.x.bx = 0x3000;           /* ax == bx == cx == dx to get */
  100.   regs.x.cx = 0x3000;           /* the MOS version number */
  101.   regs.x.dx = 0x3000;           /* if >= 4.00, use vect D4 */
  102.   int86(0x21,®s,®s);
  103.   if(regs.h.al >= 4 && regs.h.al >= 0) extvect = 0xd4;
  104.   }
  105.  
  106. /*
  107. ;- ISMOS ---------------------------------------------------------------;
  108. ; entry parms:  none                                                    ;
  109. ; exit parms:   returns == 0 if the OS is MOS                           ;
  110. ;               returns !=0 if not                                      ;
  111. ;                                                                       ;
  112. ; NOTES: Verify that this program is running under the PC-MOS           ;
  113. ; operating system before MOS specific system calls are made.           ;
  114. ;-----------------------------------------------------------------------;
  115. */
  116. int ismos() {
  117.  
  118.   word mosver;
  119.  
  120.   regs.x.ax = 0x3000;
  121.   regs.x.bx = 0x3000;
  122.   regs.x.cx = 0x3000;
  123.   regs.x.dx = 0x3000;
  124.   mosver = int86(0x21,®s,®s);
  125.   regs.x.ax = 0x3099;
  126.   return(mosver == int86( 0x21,®s,®s));
  127.   }
  128.  
  129. /*
  130. ;- DISP_STATS ----------------------------------------------------------;
  131. ; entry parms:  a pointer to the addparm structure                      ;
  132. ;               a pointer to the new task's tcb                         ;
  133. ; exit parms:   none                                                    ;
  134. ;                                                                       ;
  135. ; NOTES: this procedure displays the memory statistics after a call     ;
  136. ; is made to the addtask api function.                                  ;
  137. ;-----------------------------------------------------------------------;
  138. */
  139. void disp_stats(int newtcb, add_data *addstruc) {
  140.  
  141.   union {
  142.     int xint;
  143.     char xchar[2];
  144.     } x;
  145.  
  146.   printf("Starting task # %d\n",word_from_tcb(newtcb,tcbid));
  147.   if(addstruc->tmemtot.longint != 0) {  /* if memory management exists */
  148.     printf("Total extended memory  %ldK\n",addstruc->tmemtot.longint * 4);
  149.     printf("Extended memory allocated %ldK\n",addstruc->tmemalc.longint * 4);
  150.     }
  151.   printf("Total SMP memory  %dK\n",((long)addstruc->tsmpsiz * 16)/1024);
  152.   printf("SMP memory allocated %dK\n",((long)addstruc->tsmpal * 16)/1024);
  153.   x.xint = addstruc->tpercent;
  154.   printf("Percent of SMP in use %c%c%%\n",x.xchar[0],x.xchar[1]);
  155.   }
  156.  
  157.  
  158. /*=== Main code ===========================*/
  159.  
  160. main() {
  161.  
  162.   dword xptr;
  163.  
  164.  
  165.  
  166.   if(ismos() != 0) {
  167.     printf("\nPC-MOS must be the operating system\n");
  168.     exit(1);
  169.     }
  170.   get_vects();
  171.  
  172. /*
  173. At this point, it has been verified that the
  174. operating system is PC-MOS so it is safe to
  175. use MOS specific function calls and interface to
  176. MOS's data structures
  177. */
  178.  
  179. /* add a background task */
  180.  
  181.   addparm.tsize = 32;                   /* 32K task size */
  182.   addparm.tid = 0;                      /* let MOS pick the id */
  183.   addparm.tclass = ' ';                 /* no security */
  184.   addparm.tbatch.vptr = strtbat;
  185.   addparm.tdriver.longint = 0;
  186.   addparm.tport = 0;
  187.   addparm.tbaud.longint = 0;
  188.   xptr.vptr = &addparm;
  189.   regs.x.si = xptr.hlf[ofs];
  190.   sregs.ds = xptr.hlf[seg];
  191.   regs.h.ah = 0x22;
  192.   extcall;
  193.   if(regs.x.cflag == 1) {
  194.     printf("\nError # %d adding background task\n",regs.x.ax);
  195.     exit(1);
  196.     }
  197.   disp_stats(sregs.es,&addparm);
  198.   }
  199.  
  200.